; Main code
; Requires a segment named "ML" which
; corresponds to a memory configuration that
; starts at the same location as "MLADDR" but
; is long enough to consume all of the code.
; This segment can be renamed.
.SEGMENT "CODE"
.EXPORT _twistCharMLPtoA, _twistCharMLAtoP

.proc _twistCharMLPtoA

; Original value is in $EA

        lda $EA

        cmp #3
        bne :+
        lda #27
        jmp done

:       cmp #17     ; 17 -> 0
        bne :+
        lda #0
        jmp done

:       cmp #20     ; 20 -> 127
        bne :+
        lda #127
        jmp done

:       clc         ; 28-31 -> 0
        cmp #28
        bcs :+
        jmp :+++
:       cmp #32
        bcc :+
        jmp :++
:       lda #0
        jmp done

:       cmp #65     ; 65-90 -> 97-122
        bcs :+
        jmp :+++
:       cmp #91
        bcc :+
        jmp :++
:       clc
        adc #32
        jmp done

:       cmp #95     ; 95 -> 96
        bne :+
        lda #'-'
        jmp done

:       cmp #96     ; 96 -> 95
        bne :+
        lda #'-'
        jmp done

:       cmp #97     ; 97-122 -> 65-90
        bcs :+
        jmp :+++
:       cmp #123
        bcc :+
        jmp :++
:       sec
        sbc #32
        jmp done

:       cmp #192     ; 193-219 -> 65-90
        bcs :+
        jmp :+++
:       cmp #219
        bcc :+
        jmp :++
:       sec
        sbc #128
        jmp done

:       cmp #123    ; 123 -> 43
        bne :+
        lda #43
        jmp done

:       cmp #125    ; 125 -> 124
        bne :+
        lda #124
        jmp done

:       cmp #126    ; 126 -> 32
        bne :+
        lda #32
        jmp done

:       cmp #127    ; 123 -> 32
        bne :+
        lda #32
        jmp done

:       cmp #194    ; 123 -> 32
        bne :+
        lda #'|'
        jmp done

:       cmp #192    ; 123 -> 32
        bne :+
        lda #'-'
        jmp done

:       cmp #171    ; 123 -> 32
        bne :+
        lda #'+'
        jmp done

:       cmp #173    ; 123 -> 32
        bne :+
        lda #'+'
        jmp done

:       cmp #174    ; 123 -> 32
        bne :+
        lda #'+'
        jmp done

:       cmp #176    ; 123 -> 32
        bne :+
        lda #'+'
        jmp done

:       cmp #177    ; 123 -> 32
        bne :+
        lda #'+'
        jmp done

:       cmp #178    ; 123 -> 32
        bne :+
        lda #'+'
        jmp done

:       cmp #179    ; 123 -> 32
        bne :+
        lda #'+'
        jmp done

:       cmp #189    ; 123 -> 32
        bne :+
        lda #'+'
        jmp done

:       clc
        cmp #128
        bcs :+
        jmp done
:       lda #0

done:
        sta $EA
        rts
.endproc

.proc _twistCharMLAtoP

; Original value is in $EA

        lda $EA

        cmp #27
        bne :+
        lda #3
        jmp done

:       cmp #127     ; 20 -> 127
        bne :+
        lda #20
        jmp done

:       cmp #65     ; 65-90 -> 193-218
        bcs :+
        jmp :+++
:       cmp #91
        bcc :+
        jmp :++
:       clc
        adc #128
        jmp done

:       cmp #95     ; 95 -> 96
        bne :+
        lda #96
        jmp done

:       cmp #96     ; 96 -> 95
        bne :+
        lda #95
        jmp done

:       cmp #97     ; 97-122 -> 65-90
        bcs :+
        jmp :+++
:       cmp #123
        bcc :+
        jmp :++
:       sec
        sbc #32
        jmp done

:       cmp #43    ; 123 -> 43
        bne :+
        lda #123
        jmp done

:       cmp #124    ; 125 -> 124
        bne :+
        lda #125
        jmp done

:       lda $EA

done:
        sta $EA
        rts
.endproc